package cn.dengta.webapp.trade.dto;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;

import cn.dengta.common.model.TimeInterval;
import cn.dengta.context.model.WesKit;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.EasyDate;

/**
 * 女用户收益任务配置
 *
 * @author lizhichao
 * @since 2023-12-28
 */
@Setter
@Getter
public class FemaleIncomeTaskConfig {

	/** 每日任务 */
	public DailyTask dailyTask;
	/** 区间任务 */
	public IntervalTask intervalTask;
	/** 启用的分包,不在次Set中的，分包用户不结算 */
	private Set<WesKit> wesKitEnable;

	public List<TaskConfig> getTaskConfig(Date regisTime, Date now) {

		List<TaskConfig> taskConfigs = new ArrayList<>(2);
		TaskConfig dailyTaskConfig = dailyTask.config(regisTime, now);
		if (dailyTaskConfig != null) {
			taskConfigs.add(dailyTaskConfig);
		}
		TaskConfig intervalTaskConfig = intervalTask.config(now);
		if (intervalTaskConfig != null) {
			taskConfigs.add(intervalTaskConfig);
		}
		return taskConfigs;
	}

	public static FemaleIncomeTaskConfig getDefault() {
		FemaleIncomeTaskConfig config = new FemaleIncomeTaskConfig();
		config.setDailyTask(DailyTask.getDefault());
		config.setIntervalTask(IntervalTask.getDefault());
		return config;
	}

	/**
	 * 每日任务
	 */
	@Setter
	@Getter
	public static class DailyTask {

		/** 任务状态：0=关闭；1=启用 */
		Boolean enable;
		/** 新用户定义时间 */
		Integer userDefTime;
		/** 新用户任务是否启用 */
		Boolean newEnable;
		/** 老用户任务是否启用 */
		Boolean oldEnable;
		/** 新用户门槛 */
		List<ThresholdConfig> newThreshold;
		/** 老用户门槛 */
		List<ThresholdConfig> oldThreshold;

		public static DailyTask getDefault() {
			DailyTask dailyTask = new DailyTask();
			List<ThresholdConfig> threshold = Collections.singletonList(ThresholdConfig.getDefault());
			dailyTask.setNewThreshold(threshold);
			dailyTask.setOldThreshold(threshold);
			dailyTask.setOldEnable(false);
			dailyTask.setNewEnable(false);
			dailyTask.setUserDefTime(1);
			dailyTask.setEnable(false);
			return dailyTask;
		}

		public Boolean getNewEnable() {
			return Boolean.TRUE.equals(newEnable);
		}

		public Boolean getOldEnable() {
			return Boolean.TRUE.equals(oldEnable);
		}

		public TaskConfig config(Date regisTime, Date now) {

			if (!getEnable()) {
				return null;
			}
			// 计算注册多少天
			long daysBetween = differentDaysByMillisecond(regisTime, now);
			// 是否老用户
			boolean isOldUser = daysBetween > userDefTime;

			TaskConfig taskConfig = new TaskConfig();
			taskConfig.setStatus(1);
			if (isOldUser && oldEnable) {
				taskConfig.setInterval(TimeInterval.of(now, Calendar.DATE));
				taskConfig.setThresholdConfigs(oldThreshold);
				return taskConfig;
			}

			if (!isOldUser && newEnable) {
				taskConfig.setInterval(TimeInterval.of(now, Calendar.DATE));
				taskConfig.setThresholdConfigs(newThreshold);
				return taskConfig;
			}
			return null;
		}

		public Boolean getEnable() {
			return enable == null || enable;
		}

	}

	/**
	 * 区间任务
	 */
	@Setter
	@Getter
	public static class IntervalTask {

		/** 状态 */
		Boolean enable;
		/** 时间区间 */
		TimeInterval interval;
		/** 任务要求 */
		List<ThresholdConfig> threshold;

		public static IntervalTask getDefault() {
			IntervalTask intervalTask = new IntervalTask();
			List<ThresholdConfig> threshold = Collections.singletonList(ThresholdConfig.getDefault());
			TimeInterval interval = new TimeInterval();
			interval.setBegin(EasyDate.smartParseDate("2023-12-29"));
			interval.setEnd(EasyDate.smartParseDate("2023-12-29"));
			intervalTask.setInterval(interval);
			intervalTask.setThreshold(threshold);
			intervalTask.setEnable(false);
			return intervalTask;
		}

		public boolean isInInterval(Date date) {
			return !(date.before(interval.getFinalBegin()) || date.after(interval.getFinalEnd()));
		}

		public TaskConfig config(Date now) {
			if (!getEnable() || !isInInterval(now)) {
				return null;
			}
			TaskConfig taskConfig = new TaskConfig();
			taskConfig.setInterval(interval);
			taskConfig.setThresholdConfigs(threshold);
			taskConfig.setStatus(2);
			return taskConfig;
		}

		public Boolean getEnable() {
			return enable == null || enable;
		}

	}

	@Setter
	@Getter
	public static class ThresholdConfig {

		/** 奖励数 */
		Integer awardNum;
		/** 收益门槛 */
		Integer incomeThreshold;

		public static ThresholdConfig getDefault() {
			ThresholdConfig thresholdConfig = new ThresholdConfig();
			thresholdConfig.setIncomeThreshold(1);
			thresholdConfig.setAwardNum(1);
			return thresholdConfig;
		}

	}

	public static long differentDaysByMillisecond(Date date1, Date date2) {
		// 将 Date 转换为 LocalDate
		LocalDate localDate1 = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		LocalDate localDate2 = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
		// 计算两个 LocalDate 之间的天数
		return ChronoUnit.DAYS.between(localDate1, localDate2);
	}

	@Setter
	@Getter
	public static class TaskConfig {

		/** 收益区间 */
		TimeInterval interval;
		/** 奖励配置 */
		List<ThresholdConfig> thresholdConfigs;
		/** 状态 */
		Integer status;

	}

	public boolean isEnable(WesKit wesKit) {
		if (wesKitEnable == null) {
			return false;
		}
		return wesKitEnable.contains(wesKit);
	}

	public boolean setEnable(WesKit wesKit) {
		if (wesKitEnable == null) {
			wesKitEnable = new HashSet<>();
		}
		return wesKitEnable.add(wesKit);
	}

}
